package org.budo.support.java.io.util;

import java.io.File;
import java.util.Comparator;

/**
 * @author limingwei
 */
public class FileLastModifiedComparator implements Comparator<File> {
    public static final Comparator<File> ASC = new FileLastModifiedComparator(1);

    public static final Comparator<File> DESC = new FileLastModifiedComparator(-1);

    private int sort;

    private FileLastModifiedComparator(int sort) {
        this.sort = sort;
    }

    @Override
    public int compare(File o1, File o2) {
        long diff = o2.lastModified() - o1.lastModified();

        if (diff == 0) {
            return 0;
        }

        if (diff > 0) {
            return -1 * sort;// 倒序正序控制
        }

        return 1 * sort;// 倒序正序控制
    }
}